#include "memory.h"

#define TOKEN_SIZE sizeof(struct TOKEN)

static TOKEN *ini;
static TOKEN *cur;
static TOKEN *end;

static int tokensByRealloc = 256;


void expand_buffer(void);

void init_token_buffer(void)
{
	ini = malloc(tokensByRealloc * TOKEN_SIZE);
	cur = ini;
	end = ini + tokensByRealloc;
}

inline char* get_string(char *src, size_t size)
{
	return (char*) strndup(src, size);
}

char* save_token(int type, char *src, size_t size)
{
	expand_buffer();
	
	cur->str  = get_string(src, size);
	cur->type = type;
	cur++;
	
	return src + size;
}

void expand_buffer(void)
{
	TOKEN *p;
	size_t s;

	if (cur == end)
	{
		size_t ls = (end - ini);
		s = ls + tokensByRealloc;
		p = realloc(ini, (s) * TOKEN_SIZE);

        cur = p + ls;
        ini = p;
        end = p + (s);

		printf("[scanner: token buffer increased to %d]\n", ls);
	}
}

void print_tokens()
{
    TOKEN *i = ini;
    
	printf("\ntotal tokens:%d\n", cur - ini);

	while (i < cur)
	{
		printf(" type:%08X\tstring:%s\n", i->type, i->str);
		++i;
	}
}

void free_token_buffer()
{
    TOKEN *i = ini;

    while (i < end)
    {
        if (i->str)
            free(i->str);
        ++i;
    }

    free(ini);
    ini = cur = end = NULL;
}
